/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.openbravo.bean.erp;

import cn.ekuma.data.dao.bean.CompositeKey;
import cn.ekuma.data.dao.bean.I_AutoGeneratorStringKey;
import cn.ekuma.data.dao.bean.I_ModifiedLogBean;
import com.openbravo.bean.Product;
import com.openbravo.bean.Serving;
import com.openbravo.bean.Tax;
import com.openbravo.bean.shard.I_CalcLine;
import com.openbravo.def.I_TaxCalcLine;

import java.util.Date;
import java.util.HashMap;

/**
 *
 * @author Administrator
 */
public class OrderLine implements I_ModifiedLogBean<String>,I_TaxCalcLine,I_CalcLine,I_AutoGeneratorStringKey{

    static final long serialVersionUID=-2464564805378645676L;
    
    public static int PRODUCTLINE=0;
    public static int SERVICELINE=1;
    public static int CASHLINE=2;
    public String id;
    public String m_sOrder;
    public int m_iLine;
    public double multiply;
    public double price;//实售价
    public Tax tax;
    public HashMap<String,String> attributes;
    public String productid;
    public String attsetinstid;
    private String lotNo;
    private Date validityDate;
    

    public double defaultPrice;//此行商品的原价（可以是普通价或会员价），是进行二次计算的基础价
    public double priceRate;//单行折扣率(如果为整单折扣的，此值为1，否则为单行折扣
    public boolean isPrefSell;//用于断定当前销售的商品是否是特价商品

    public static String OrderlINE_EXTPROP="extprop";
    public static String ORDERLINE_pageProperty="pageNum";//用于设置当前行所属的页属性.

    public double consulMultiply;//参考量

    private boolean isFilter;
    private Date lastModified;
    
    public double consultPrice;
    private double attrPrice;// 属性差价
    private double attrPriceCost;//成本


    public OrderLine() {
        init(null, null, 0.0, 0.0, null, new HashMap(),1,0);
    }

    public OrderLine(Serving servings,Tax tax,double rate){
        init(null, null, 1, servings.getCyclePrice(), tax, new HashMap(),rate,0);
        setProperty(PROPERTY_ServingsCyclesType, servings.getRantCycleStyle());
        setProperty(PROPERTY_ServingsStart, String.valueOf(servings.getRantData().getTime()));
        setProperty("product.name", servings.getM_sName());
        setProperty(PROPERTY_ServingsId,servings.getM_sServiceId());
    }

    public OrderLine(Product product, double dMultiply, double dPrice, Tax tax,
			HashMap attributes, double rate, double consultPrice) {
    	String pid= product.getID(); 
        init(pid, null, dMultiply, dPrice, tax, attributes,rate,consultPrice);
        checkProduct(product);
	}

	public void init(String productid, String attsetinstid, double dMultiply, double dPrice, Tax tax, HashMap attributes,double rate,double consultPrice) {
        this.productid = productid;
        this.attsetinstid = attsetinstid;
        multiply = dMultiply;
        price = dPrice;
        this.tax = tax;
        this.attributes = attributes;

        defaultPrice=price;
        priceRate=rate;
        isPrefSell=false;

        isFilter=false;
        m_sOrder = null;
        m_iLine = -1;
        consulMultiply=0;
        this.consultPrice=consultPrice;  
        attrPrice=0;
        attrPriceCost=0;
    }

	public void checkProduct(Product product){
		attributes.put("product.name", product.getName());
        attributes.put("product.com", product.isCom() ? "true" : "false");
        if (product.getAttributeSetID() != null) {
            attributes.put("product.attsetid", product.getAttributeSetID());
        }
        attributes.put("product.taxcategoryid", product.getTaxCategoryID());
        if (product.getCategoryID() != null) {
            attributes.put("product.categoryid", product.getCategoryID());
        }
        attributes.put("product.buyPrice", String.valueOf(product.getPriceBuy()));
        attributes.put("product.sellPrice", String.valueOf(product.getPriceSell()));
        attributes.put("product.customerPrice", String.valueOf(product.getM_dCustomerPrice()));
        attributes.put("product.code", product.getCode());
        attributes.put("product.manufacturing", product.isM_bManufacturing() ? "true" : "false");
        attributes.put("product.wight", String.valueOf(product.getWeight()));
        attributes.put("product.quantity", String.valueOf(product.getM_quantity()));
        attributes.put("product.isBomStore", product.isBomStore()? "true" : "false");
	}

    public void setOrder(String Order, int line) {
        m_sOrder = Order;
        m_iLine = line;
    }

    public OrderLine copyOrderLine() {
        OrderLine l = new OrderLine();
        // l.m_sOrder = null;
        // l.m_iLine = -1;
        l.productid = productid;
        l.attsetinstid = attsetinstid;
        l.multiply = multiply;
        l.price = price;
        l.tax = tax;
        l.attributes = (HashMap) attributes.clone();

        l.isPrefSell=isPrefSell;
        l.defaultPrice=defaultPrice;
        l.priceRate=priceRate;
        l.consultPrice=consultPrice;
        l.lotNo=lotNo;
        l.validityDate=validityDate;
        l.attrPrice=attrPrice;
        l.attrPriceCost=attrPriceCost;
        return l;
    }

   public boolean isIsPrefSell() {
        return isPrefSell;
    }
   
   public boolean isManufacturing(){
		return "true".equals(getProperty("product.manufacturing"));
	}

    public void setIsPrefSell(boolean isPrefSell) {
        this.isPrefSell = isPrefSell;
    }


    public double getPriceRate() {
        return priceRate;
    }

    public void setPriceRate(double rate) {
        priceRate = rate;
    }

    public int getOrderLine() {
        return m_iLine;
    }

    public String getProductID() {
        return productid;
    }

    public String getProductBuyPrice(){
        return getProperty("product.buyPrice","");
    }

   public String getProductSellPrice(){
        return getProperty("product.sellPrice","");
    }

    public String getProductCustomerPrice(){
        return getProperty("product.customerPrice","");
    }
    
    public String getProductName() {
        return getProperty("product.name");
    }

    public String getProductAttSetId() {
        return getProperty("product.attsetid");
    }

    public String getProductAttSetInstDesc() {
        return getProperty("product.attsetdesc", "");
    }
    
    public double getProductQuantity(){
    	String prop=getProperty("product.quantity","1");
    	return new Double(prop);
    }

    public void setProductAttSetInstDesc(String value) {
        if (value == null) {
            attributes.remove(value);
        } else {
            attributes.put("product.attsetdesc", value);
        }
    }

    public int getPageIndex(){
        return Integer.valueOf(getProperty(ORDERLINE_pageProperty, "0"));
    }

    public void setPageIndex(int pageIndex){
        attributes.put(ORDERLINE_pageProperty,String.valueOf(pageIndex));
    }

    public String getProductAttSetInstId() {
        return attsetinstid;
    }

    public void setProductAttSetInstId(String value) {
        attsetinstid = value;
    }

    public boolean isProductCom() {
        return "true".equals(getProperty("product.com"));
    }
    
    public boolean isBomStore(){
    	return "true".equals(getProperty("product.isBomStore","true"));
    }

    public String getProductTaxCategoryID() {
        return getProperty("product.taxcategoryid");
    }

    public String getProductCategoryID() {
        return getProperty("product.categoryid");
    }

    public String getProductCode() {
        return getProperty("product.code","");
    }

    public double getMultiply() {
        return multiply;
    }

    public void setMultiply(double dValue) {
        multiply = dValue;
    }

    public Tax getTaxInfo() {
        return tax;
    }

    public void setTaxInfo(Tax value) {
        tax = value;
    }

    public double getConsulMultiply() {
        return consulMultiply;
    }

    public void setConsulMultiply(double consulMultiply) {
        this.consulMultiply = consulMultiply;
    }

    public String getProperty(String key) {
        return attributes.get(key);
    }

    public String getProperty(String key, String defaultvalue) {
        String ret=attributes.get(key);
        return ret!=null?ret:defaultvalue;
    }

    public void setProperty(String key, String value) {
        getProperty(key, value);
    }

    public HashMap getProperties() {
        return attributes;
    }

    public double getTaxRate() {
        return tax == null ? 0.0 : tax.getRate();
    }

    public double getDefaultPrice() {
        return defaultPrice;
    }

    public void setDefaultPrice(double defaultPrice) {
        this.defaultPrice = defaultPrice;
    }

    public boolean isServiceOrderLine(){
       return attributes.containsKey(PROPERTY_ServingsId);
    }

    public int getLineStyle() {
        if(productid!=null)
            return OrderLine.PRODUCTLINE;
        if(isServiceOrderLine())
            return OrderLine.SERVICELINE;
        return OrderLine.CASHLINE;
    }

    public void setFilter(boolean b){
         this.isFilter=b;
    }

    public boolean isFilter(){
        return isFilter;
    }

    public Date getLastModified() {
        return lastModified;
    }

    public void setLastModified(Date lastModified) {
        this.lastModified = lastModified;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double m_dPrice) {
        this.price=m_dPrice;
    }

    public void setPriceTax(double priceTax) {
        price = priceTax / (1.0 + getTaxRate());
    }

    public String getRantCycleStyle() {
        return this.getProperty(PROPERTY_ServingsCyclesType);
    }

    public Date getStartData() {
         String startDataStr=getProperty(PROPERTY_ServingsStart);
         if(startDataStr!=null)
             return new Date(Long.parseLong(startDataStr));
         return null;
    }

    public void setStartData(Date start){
       attributes.put(PROPERTY_ServingsStart, String.valueOf(start.getTime()));
    }

    public void setEndData(Date end){
        attributes.put(PROPERTY_ServingsEnd, String.valueOf(end.getTime()));
    }
    
    public Date getEndData() {
        String endDataStr=getProperty(PROPERTY_ServingsEnd);
         if(endDataStr!=null)
             return new Date(Long.parseLong(endDataStr));
         return null;
    }

    public String getServiceId(){
        return getProperty(PROPERTY_ServingsId);
    }

    public void setServing(Serving servings){
        setProperty(PROPERTY_ServingsCyclesType, servings.getRantCycleStyle());
        setProperty(PROPERTY_ServingsStart, String.valueOf(servings.getRantData().getTime()));
        if(servings.getReturnData()!=null)
            setProperty(PROPERTY_ServingsEnd, String.valueOf(servings.getReturnData().getTime()));
        setProperty("product.name", servings.getM_sName());
        setProperty(PROPERTY_ServingsId,servings.getM_sServiceId());
        this.price=servings.getCyclePrice();
        this.priceRate=servings.getRantPrice();
    }

	public double getConsultPrice() {
		return consultPrice;
	}

	public void setConsultPrice(double consultPrice) {
		this.consultPrice = consultPrice;
	}

	public String getLotNo() {
		return lotNo;
	}

	public void setLotNo(String lotNoId) {
		this.lotNo = lotNoId;
	}

	public Date getValidityDate() {
		return validityDate;
	}

	public void setValidityDate(Date validityDate) {
		this.validityDate = validityDate;
	}

	@Override
	public void setKey(String key) {
		this.id=key;	
	}

	@Override
	public String getKey() {	
		return id;
	}

	public double getAttrPrice() {
		return attrPrice;
	}

	public void setAttrPrice(double attrPrice) {
		this.attrPrice = attrPrice;
	}

	public double getAttrPriceCost() {
		return attrPriceCost;
	}

	public void setAttrPriceCost(double attrPriceCost) {
		this.attrPriceCost = attrPriceCost;
	}
}
